package com.cmccgz.udacs_landlord_webapp.main.cardmanager;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.cmccgz.baselibrary.utils.RegularUtil;
import com.cmccgz.udacs_landlord_webapp.bean.AccreditCardBean;
import com.cmccgz.udacs_landlord_webapp.bean.IdBean;
import com.cmccgz.udacs_landlord_webapp.data.BaseCallback;
import com.cmccgz.udacs_landlord_webapp.data.HouseManagerRepository;
import com.cmccgz.udacs_landlord_webapp.data.IdScanDataRepository;
import com.cmccgz.udacs_landlord_webapp.data.IdScanDataSource;
import com.orhanobut.logger.Logger;
import com.youtu.Youtu;

/**
 * 开卡授权Presenter
 */
public class CardAccreditPresenter implements CardAccreditContract.Presenter {

    //认证信息
    private String APP_ID = "10102888";
    private String SECRET_ID = "AKID1pSq0vO2FDwQfgQ1JL7duuDI8k7FomNq";
    private String SECRET_KEY = "PccMq42kBQRCixwiEVaPyOaXEfqlUxB9";

    private CardAccreditContract.View mCardAccreditView;
    private HouseManagerRepository mRepository;
    private String[] nations = new String[]{"汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "朝鲜族", "满族", "侗族", "瑶族", "白族", "土家族",
            "哈尼族", "哈萨克族", "傣族", "黎族", "傈僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族",
            "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "塔吉克族", "怒族", "乌孜别克族",
            "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族"};
    private Youtu youtu;
    private IdScanDataRepository idScanDataRepository;

    public CardAccreditPresenter(@NonNull CardAccreditContract.View cardAccreditView, HouseManagerRepository repository) {
        mCardAccreditView = cardAccreditView;
        mRepository = repository;
        mCardAccreditView.setPresenter(this);
    }

    @Override
    public void start() {

    }

    @Override
    public void selectNation() {
        mCardAccreditView.showAllNation(nations);
    }

    @Override
    public void completeCardAccredit(String mobile, String usermobile, String name, String sex, String idNumber, String nation, String birth, String houseId, String endTime) {

        if (TextUtils.isEmpty(mobile)) {
            //TODO
            return;
        }
        Logger.d(usermobile);
        if (TextUtils.isEmpty(usermobile)) {
            mCardAccreditView.showEmptyMobile();
            return;
        }

        if (!RegularUtil.isMobileNumber(usermobile)) {
            mCardAccreditView.showInvalidMobile();
            return;
        }

        if (TextUtils.isEmpty(name)) {
            mCardAccreditView.showEmptyName();
            return;
        }
        if (name.length() < 2 || name.length() > 30) {
            mCardAccreditView.showInvalidName();
            return;
        }
        if (TextUtils.isEmpty(idNumber)) {
            mCardAccreditView.showEmptyIdNumber();
            return;
        }
        if (!RegularUtil.isIDCardNumber(idNumber)) {
            mCardAccreditView.showInvalidIdNumber();
            return;
        }
        if (TextUtils.isEmpty(nation)) {
            mCardAccreditView.showEmptyNation();
            return;
        }
        if (TextUtils.isEmpty(birth)) {
            mCardAccreditView.showEmptyBirthDate();
            return;
        }
        if (TextUtils.isEmpty(houseId)) {
            mCardAccreditView.showEmptyHouse();
            return;
        }
        if (TextUtils.isEmpty(endTime)) {
            mCardAccreditView.showEmptyEndTime();
            return;
        }
        mCardAccreditView.showLoadingDialog();

        AccreditCardBean bean = new AccreditCardBean();
        bean.setMobile(mobile);
        bean.setUser_name(name);
        bean.setSex(sex);
        bean.setUser_mobile(usermobile);
        bean.setId_card(idNumber);
        bean.setUser_nation(nation);
        bean.setUser_bir(birth);
        bean.setHouse_id(houseId);
        bean.setHouse_end_time(endTime);
        mRepository.accreditCard(bean, new BaseCallback() {
            @Override
            public void onSuccess() {
                mCardAccreditView.hideLoadingDialog();
                mCardAccreditView.showAccreditCardSuccess();
            }

            @Override
            public void onFailed(String msg) {
                mCardAccreditView.hideLoadingDialog();
                mCardAccreditView.showAccreditCardError(msg);
            }
        });

    }

    @Override
    public void scanId(final String imgUrl) {
        if (idScanDataRepository==null) {
            idScanDataRepository = new IdScanDataRepository();
        }
        idScanDataRepository.scanId(imgUrl, new IdScanDataSource.IdScanLisenter() {
            @Override
            public void scanIdSuccess(IdBean idBean) {
                mCardAccreditView.scanIdSuccess(idBean);
            }

            @Override
            public void scanIdError(String msg) {
                mCardAccreditView.scanIdError(msg);
            }
        });
      /*  Observable<String> observable = Observable.create(new AsyncOnSubscribe<String, String>() {
            @Override
            protected String generateState() {
                Log.e("inx", "generateState: ");
                return null;
            }

            @Override
            protected String next(String state, long requested, Observer<Observable<? extends String>> observer) {
                Log.e("inx", "Observable.create-next: " );
                return imgUrl;
            }
        });
        observable.just(imgUrl)
                .subscribeOn(Schedulers.newThread())
                .map(new Func1<String, IdBean>() {
                    @Override
                    public IdBean call(String s) {
                        Log.e("inx", "call: s="+s);
                        Youtu youtu = getYoutu();
                        IdBean idBean=null;
                        try {
                            JSONObject jsonObject = youtu.IdCardOcr(imgUrl, 0);
                            Log.e("inx", "call: jsonObject="+jsonObject);
                            String name=jsonObject.getString("name");
                            String sex=jsonObject.getString("sex");
                            String nation=jsonObject.getString("nation");
                            String birth=jsonObject.getString("birth");
                            String id=jsonObject.getString("id");
                            String address=jsonObject.getString("address");
                            idBean=new IdBean(id,name,sex,birth,nation,address);
                        } catch (Exception e) {
                            e.printStackTrace();
                            Log.e("inx", "call: e="+e);
                        }
                        Log.e("inx", "call: idBean="+idBean);
                        return idBean;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<IdBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mCardAccreditView.scanIdError("解析失败！请选择高清身份证正面照再次操作！");
                    }

                    @Override
                    public void onNext(IdBean idBean) {
                        mCardAccreditView.scanIdSuccess(idBean);
                    }
                });*/
    }

    private Youtu getYoutu() {
        if (youtu == null) {
            youtu = new Youtu(APP_ID, SECRET_ID, SECRET_KEY, Youtu.API_YOUTU_END_POINT);
        }
        return youtu;
    }


}
